package com.estore.service;

import com.com.estore02.model.Product;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品服务类 - 服务器端使用
 */
public class ProductService {
    private final Map<String, Product> productData = new HashMap<>();
    private String dataFilePath = "products.dat";

    public ProductService() {
        loadConfig();
        loadData();

        if (productData.isEmpty()) {
            initializeDefaultProducts();
        }
    }

    private void loadConfig() {
        // 简化的配置加载
        dataFilePath = "products.dat";
    }

    @SuppressWarnings("unchecked")
    private void loadData() {
        File dataFile = new File(dataFilePath);
        if (!dataFile.exists()) {
            return;
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(dataFilePath))) {
            Map<String, Product> loadedData = (Map<String, Product>) ois.readObject();
            productData.clear();
            productData.putAll(loadedData);
            System.out.println("✅ 商品数据加载成功: " + productData.size() + " 个商品");
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("❌ 数据加载失败: " + e.getMessage());
        }
    }

    public void saveData() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(dataFilePath))) {
            oos.writeObject(productData);
        } catch (IOException e) {
            System.out.println("❌ 数据保存失败: " + e.getMessage());
        }
    }

    private void initializeDefaultProducts() {
        productData.put("p001", new Product("p001", "Java从入门到精通", 99.0, 100));
        productData.put("p002", new Product("p002", "高性能MySQL", 128.5, 50));
        productData.put("p003", new Product("p003", "颈椎康复指南", 45.0, 200));
        productData.put("p004", new Product("p004", "生发秘籍", 88.0, 0));
        productData.put("p005", new Product("p005", "Python编程", 79.0, 80));
        productData.put("p006", new Product("p006", "数据结构与算法", 120.0, 30));
        saveData();
    }

    // 业务方法保持不变
    public List<Product> getAllProducts() {
        return new ArrayList<>(productData.values());
    }

    public Product findProductById(String id) {
        return productData.get(id);
    }

    public boolean isProductExist(String id) {
        return productData.containsKey(id);
    }

    public boolean isStockSufficient(String id, int quantity) {
        Product product = productData.get(id);
        return product != null && product.getStock() >= quantity;
    }

    public void reduceStock(String id, int quantity) {
        Product product = productData.get(id);
        if (product != null) {
            product.setStock(product.getStock() - quantity);
            saveData();
        }
    }

    public int getTotalProductCount() {
        return productData.size();
    }

    public List<Product> getAvailableProducts() {
        List<Product> availableProducts = new ArrayList<>();
        for (Product product : productData.values()) {
            if (product.getStock() > 0) {
                availableProducts.add(product);
            }
        }
        return availableProducts;
    }
}